1 package org.smartcomps.twister.engine.core.dynamic;
2
3 import junit.framework.TestCase;
4 import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
5 import net.sf.hibernate.cfg.Configuration;
6
7 import org.smartcomps.twister.common.transaction.TransactionManager;
8 import org.smartcomps.twister.common.persistence.XMLDataAccess;
9 import org.smartcomps.twister.common.lifecycle.LifecycleManager;
10
11 import org.smartcomps.twister.engine.core.definition.TestProcess;
12 import org.smartcomps.twister.engine.core.definition.TestAssign;
13 import org.smartcomps.twister.engine.priv.core.dynamic.ProcessInstance;
14 import org.smartcomps.twister.engine.priv.core.dynamic.ExecutionContext;
15 import org.smartcomps.twister.engine.priv.core.dynamic.ProcessInstanceFactory;
16 import org.smartcomps.twister.engine.priv.core.dynamic.ReceiveEC;
17 import org.smartcomps.twister.engine.priv.core.dynamic.SequenceEC;
18 import org.smartcomps.twister.engine.priv.core.dynamic.impl.xao.VariableXAO;
19 import org.smartcomps.twister.engine.priv.core.dynamic.impl.ProcessInstanceImpl;
20 import org.smartcomps.twister.engine.priv.core.definition.*;
21 import org.smartcomps.twister.deployer.TwisterDeployer;
22 import org.smartcomps.twister.deployer.TwisterDeployerFactory;
23
24 import org.dom4j.Document;
25 import org.dom4j.DocumentHelper;
26 import org.dom4j.Element;
27
28 import java.util.Map;
29 import java.util.HashMap;
30
31 /***
32 * Testing the Assign execution.
33 */
34 public class TestAssignEC extends TestCase {
35
36 private TwisterDeployer deployer;
37
38 private TestProcess testProcess = new TestProcess();
39 private TestAssign testAssign = new TestAssign();
40
41 protected void setUp() throws Exception {
42 LifecycleManager.getLifecycleManager().createResources();
43 LifecycleManager.getLifecycleManager().startResources();
44
45 SchemaExport schemaExport = new SchemaExport(new Configuration().configure());
46 schemaExport.create(true, true);
47
48 deployer = TwisterDeployerFactory.getTwisterDeployer();
49 }
50
51 protected void tearDown() throws Exception {
52 deployer = null;
53
54 LifecycleManager.getLifecycleManager().stopResources();
55 LifecycleManager.getLifecycleManager().destroyResources();
56 }
57
58 public void testExecute() throws Exception {
59 TransactionManager.beginTransaction();
60 XMLDataAccess.cleanCollection("/testProcess");
61 testProcess.testCreateWithCorrelation();
62 testAssign.testCreate();
63
64 // Executing the first receive, so that we can set variable during it's waiting.
65 Map corrProp = new HashMap();
66 corrProp.put(TestProcess.CORRELATION_PROP1, "2578");
67 corrProp.put(TestProcess.CORRELATION_PROP2, "12");
68 Receive firstAct = (Receive)((Sequence) ProcessFactory.getByName(TestProcess.testProcess.getName()).getActivity()).getActivities().get(0);
69 firstAct.execute(TestProcess.CORRELATION_NAME, corrProp);
70
71 TransactionManager.commitTransaction();
72 TransactionManager.beginTransaction();
73
74 // Setting the variables and deblocking the receive to have the assign executed.
75 ProcessInstance createdInstance = ProcessInstanceFactory.findInstanceByCorrelation(TestProcess.CORRELATION_NAME, corrProp);
76 Document doc1 = DocumentHelper.createDocument();
77 Element root1 = doc1.addElement("message");
78 root1.addElement("part11").addElement("subpart1").setText("someText");
79 root1.addElement("part12").setText("part2");
80
81 Document doc2 = DocumentHelper.createDocument();
82 Element root2 = doc2.addElement("message");
83 root2.addElement("part21").addElement("subpart2").setText("someOtherText");
84 root2.addElement("part22").setText("part22");
85
86 VariableXAO.createVariable(TestProcess.testProcess.getName(), "variable1", ((ProcessInstanceImpl)createdInstance).getId(), doc1);
87 VariableXAO.createVariable(TestProcess.testProcess.getName(), "variable2", ((ProcessInstanceImpl)createdInstance).getId(), doc2);
88
89 System.out.println("1=> " + createdInstance.getChildExecutionContext());
90 System.out.println("2=> " + ((SequenceEC)createdInstance.getChildExecutionContext()).getExecutionContexts().get(0));
91 ((ReceiveEC)((SequenceEC)createdInstance.getChildExecutionContext()).getExecutionContexts().get(0)).acknowledgeMessage(null);
92
93 // Checking execution is done
94 createdInstance = ProcessInstanceFactory.findInstanceByCorrelation(TestProcess.CORRELATION_NAME, corrProp);
95 assertEquals("Process is not completed after execution ended", ProcessInstance.COMPLETED, createdInstance.getStatus());
96 assertEquals("Assign is not completed after execution ended", ExecutionContext.COMPLETED, createdInstance.getChildExecutionContext().getStatus());
97
98 // Check that variables have been updated
99 Document resultDoc = XMLDataAccess.getDocument("/" + TestProcess.testProcess.getName() +
100 "/variable/", "variable2" + ((ProcessInstanceImpl)createdInstance).getId());
101 System.out.println("Result doc : " + resultDoc.asXML());
102 Element newPart = resultDoc.getRootElement().element("part21");
103 assertNotNull("Could not find the newly assigned part in the variable", newPart);
104 assertNotNull("The newly assigned part doesn't include the expected element", newPart.element("subpart1"));
105
106 TransactionManager.commitTransaction();
107 }
108
109 public void testSequentialAssigns() throws Exception {
110 deployer.deploy(getClass().getClassLoader().getResource("test-assign.xml"));
111
112 TransactionManager.beginTransaction();
113
114 TwisterProcess process = ProcessFactory.getByName("test-assign");
115 // Adding property explicitly as the deployer doesn't handle it yet
116 ProcessFactory.addProperty(process, "numberprop", "xsd:int", "assignVarType", "mainpart", "/number");
117
118 // Getting first assign in the process and executing it
119 ExecutionContext context =
120 ((Assign)((Sequence)process.getActivity()).getActivities().get(0)).execute(null, null);
121
122 TransactionManager.commitTransaction();
123 TransactionManager.beginTransaction();
124
125 // Checking that the last assigned variable has the expected value
126 Document resultVar = VariableXAO.getVariable("test-assign", "assignVar4", context.fetchInstance().getId());
127 System.out.println("Obtained variable : " + resultVar.asXML());
128 assertEquals("Result variable is holding a wrong value", "2.0",
129 resultVar.getRootElement().element("mainpart").element("number").getText());
130 assertEquals("Instance is not complete", ProcessInstance.COMPLETED, context.fetchInstance().getStatus());
131
132 TransactionManager.commitTransaction();
133 }
134 }
This page was automatically generated by Maven